package com.ysxsoft.wdg.ui.activity.center_st;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.umeng.socialize.UMShareAPI;
import com.youth.banner.Banner;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.PingLunAdapter;
import com.ysxsoft.wdg.adapter.center_st.StGoodsAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.DiscussBean;
import com.ysxsoft.wdg.bean.response.CollectShopResponse;
import com.ysxsoft.wdg.bean.st.CompanyBean;
import com.ysxsoft.wdg.bean.st.CompanyDetailBean;
import com.ysxsoft.wdg.bean.st.StProductBean;
import com.ysxsoft.wdg.bean.st.StShopDetailBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.widget.SharePopupWindow;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.Call;

public class StShopDetailActivity extends BaseActivity {


    public static void start(Context context, String shopId) {
        Intent intent = new Intent(context, StShopDetailActivity.class);
        intent.putExtra("shopId", shopId);
        context.startActivity(intent);
    }

    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.btn_collect)
    ImageView btnCollect;
    @BindView(R.id.iv_shop_logo)
    ImageView ivShopLogo;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.pic)
    ImageView pic;
    @BindView(R.id.tv_shop_pf)
    TextView tvShopPf;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_goods)
    TextView tvGoods;
    @BindView(R.id.tv_pj)
    TextView tvPj;
    @BindView(R.id.tv_null)
    TextView tvNull;
    @BindView(R.id.tv_desc)
    TextView tv_desc;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.recyclerview2)
    RecyclerView recyclerview2;
    @BindView(R.id.image)
    RelativeLayout image;

    private UMShareAPI mShareAPI;
    private SharePopupWindow popupWindow;

    private StGoodsAdapter goodsAdapter;
    private PingLunAdapter plAdapter;

    private String shopId = "", uid = "";
    private CompanyDetailBean company;
    private List<DiscussBean> discuss = new ArrayList<>();
    private List<StProductBean> product = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shop_detail;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, true);
        tvGoods.setSelected(true);

        Intent intent = getIntent();
        shopId = intent.getStringExtra("shopId");

        uid = SharePrefUtils.getUserId();

        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview2.setLayoutManager(new LinearLayoutManager(this));

        goodsAdapter = new StGoodsAdapter(R.layout.item_shop_goods);
        plAdapter = new PingLunAdapter(R.layout.item_pl);

        recyclerview.setAdapter(goodsAdapter);
        recyclerview2.setAdapter(plAdapter);

        mShareAPI = UMShareAPI.get(this);
        popupWindow = new SharePopupWindow(this, mShareAPI);
        getShopDetail();
    }

    private void getShopDetail() {
        mApiHelper.getStShopDetail(shopId, uid, new Observer<BaseBean<StShopDetailBean>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseBean<StShopDetailBean> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    StShopDetailBean data = baseBean.getData();
                    company = data.getCompany();
                    discuss = data.getDiscuss();
                    product = data.getProduct();
                    tv_desc.setText(company.getDesc() == null ? "" : company.getDesc());
//                    pic = data.getPic();
                    goodsAdapter.setNewData(product);
                    plAdapter.setNewData(discuss);

                    ViewUtils.loadRoundCircleImage(mContext, company.getPic(), ivShopLogo);
                    tvShopName.setText(company.getName());
                    tvShopPf.setText("评分" + company.getGrade() + "|月销" + company.getSold());
                    tvLocation.setText(company.getMap_address());

                    recyclerview.setVisibility(product.size() > 0 ? View.VISIBLE : View.GONE);
                    tvNull.setVisibility(product.size() > 0 ? View.GONE : View.VISIBLE);
                    btnCollect.setSelected(company.getSou() == 1);

                    //背景图片
                    Glide.with(StShopDetailActivity.this).load(company.getPic()).into(pic);
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("tag", "1");
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void setListener() {
        goodsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                StGoodsDetailActivity.start(mContext, goodsAdapter.getItem(position).getId());
            }
        });
    }

    @OnClick({R.id.btn_finish, R.id.btn_share, R.id.btn_collect,
            R.id.iv_shop_logo, R.id.tv_shop_name, R.id.tv_shop_pf,
            R.id.tv_location, R.id.tv_goods, R.id.tv_pj, R.id.clickLayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_finish:
                finish();
                break;
            case R.id.btn_share://分享
                popupWindow.setShareData(getString(R.string.app_name), "商家店铺"/*+data*/, UrlHost.BASE_URL+"/index.html#/store?store=1&gid=" + shopId, "", 0);
                popupWindow.show(mRootView);
                break;
            case R.id.btn_collect://收藏
                collectShop();
                break;
            case R.id.iv_shop_logo:
            case R.id.tv_shop_name:
            case R.id.tv_shop_pf:
            case R.id.tv_location://店铺详情
                break;
            case R.id.clickLayout://店铺详情
                StShopDetail3Activity.start(mContext, company);
                break;
            case R.id.tv_goods:
                tvGoods.setSelected(true);
                tvPj.setSelected(false);
                recyclerview2.setVisibility(View.GONE);

                recyclerview.setVisibility(product.size() > 0 ? View.VISIBLE : View.GONE);
                tvNull.setVisibility(product.size() > 0 ? View.GONE : View.VISIBLE);
                break;
            case R.id.tv_pj:
                tvGoods.setSelected(false);
                tvPj.setSelected(true);
                recyclerview.setVisibility(View.GONE);

                recyclerview2.setVisibility(discuss.size() > 0 ? View.VISIBLE : View.GONE);
                tvNull.setVisibility(discuss.size() > 0 ? View.GONE : View.VISIBLE);

                break;
        }
    }

    /**
     * 收藏店铺
     */
    private void collectShop() {
        OkHttpUtils.post()
                .url(UrlHost.COLLECT_SHOP)
                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("companyid", shopId + "")
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    CollectShopResponse collectShopResponse = JsonUtils.parseByGson(response, CollectShopResponse.class);
                    if (collectShopResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(collectShopResponse.getCode())) {
                        ToastUtils.showToast(collectShopResponse.getMs());
                        if (btnCollect.isSelected()) {
                            btnCollect.setSelected(false);
                        } else {
                            btnCollect.setSelected(true);
                        }
                    } else {
                        ToastUtils.showToast(collectShopResponse.getMs());
                    }
                } else {
                }
            }
        });
    }

}
